package javafx_20092010_reeks2.samsegers.Screencapturing.screenshotzoom;

import javafx.scene.*;
import javafx.scene.shape.Rectangle;
import javafx.stage.*;
import javafx.scene.paint.Color;
import javafx.scene.image.*;
import javafx.scene.input.*;
import javafx.animation.*;
import java.io.IOException;
import javafx_20092010_reeks2.samsegers.Screencapturing.FocusPack.FocusRequester;
import javafx.scene.effect.BlendMode;
import javafx_20092010_reeks2.samsegers.Screencapturing.AVIpack.*;
import java.io.*;
 
public class ScreenCap {

    var bgRect: Rectangle;
    var stage: Stage;
    var closer: ImageView;
    var starter: ImageView;
    var pauser: ImageView;
    var stopper: ImageView;
    var balk : Rectangle;
    var capturerGroup : Group;
    var menuBalk : Group;
    var magnifierImageView : ImageView;
    var lenseImgView : ImageView;
//    var robot : Robot = new Robot();
    var boundsOfRect : Rectangle;
    var stageDragInitialX:Number;
    var stageDragInitialY:Number;
    var screenShotTimeline : Timeline;
    var video:Video;
    var bounds:java.awt.Rectangle on replace {this.video.setBounds(bounds);};


    var stageX : Number on replace {
        screenShotTimeline.stop();
        screenShotTimeline.playFromStart();
    }
    var stageY : Number on replace {
        screenShotTimeline.stop();
        screenShotTimeline.playFromStart();
    }

	init {
            video = new Video("avifilmpje1"); //video test aanmaken
            screenShotTimeline = Timeline {
                keyFrames: [
                    KeyFrame {
                        time: 50ms
                        action: function() {
                            updateImage();
                        }
                    }
                ]
            };

            bgRect = Rectangle{
                width : Picture.getScreenDimension().getWidth()-10;
                height : Picture.getScreenDimension().getHeight()-10;
            }

            boundsOfRect = Rectangle{
            	x : bind lenseImgView.boundsInParent.minX
            	y : bind lenseImgView.boundsInParent.minY
            	width : bind lenseImgView.boundsInParent.width
            	height : bind lenseImgView.boundsInParent.height
                fill : Color.TRANSPARENT
            }

            lenseImgView = ImageView {
               cursor: Cursor.MOVE;
                blocksMouse:true
                //ffkes testen met hier te zetten
		onMousePressed: function(e) {
                    stageDragInitialX = e.screenX - stage.x;
                    stageDragInitialY = e.screenY - stage.y;
                }
                onMouseDragged: function(e) {
                    stage.x = e.screenX - stageDragInitialX;
                    stage.y = e.screenY - stageDragInitialY;
                }

               image : Image {
                    width : Picture.getScreenDimension().getWidth();
                    height : Picture.getScreenDimension().getHeight();
                    url : "{__DIR__}oie_venster3.png";
                    //url : "{__DIR__}badeye_canvas.png";
                    //url : "{__DIR__}oie_venster2.png"; voor een groot vierkant

                }
                opacity: 0.7
             }

          closer = ImageView {
                translateX: bind stage.width-20
                translateY: 0
                cursor: Cursor.HAND
                image: Image {
                    url: "{__DIR__}closer.png"
                }
                onMousePressed: function(e) {
                    FX.exit();
                }
            }
          starter = ImageView {
                translateX: 0
                translateY: bind stage.height-40
                cursor: Cursor.HAND
                blocksMouse: true
                image: Image {

                    url: "{__DIR__}play.png"
                }
                onMousePressed: function(e) {
                    try{
                        video.start(bounds);
                        starter.visible = false;
                        pauser.visible = true;
                    }
                    catch(ex : IOException) {
                        ex.printStackTrace();
                    }
                }
            }
            pauser = ImageView {
                translateX: 0
                translateY: bind stage.height-40
                cursor: Cursor.HAND
                blocksMouse: true
                visible: false
                image: Image {
                    url: "{__DIR__}pause.png"
                }
                onMousePressed: function(e) {
                    try{
                        video.pause();
                        starter.visible=true;
                        pauser.visible=false;
                    }
                    catch(ex : IOException) {
                        ex.printStackTrace();
                    }
                }
            }
          stopper = ImageView {
                translateX: 40
                translateY: bind stage.height-40
                cursor: Cursor.HAND
                blocksMouse: true
                image: Image {
                    url: "{__DIR__}stop.png"
                }
                onMousePressed: function(e) {
                        video.stop();
                        starter.visible=true;
                }
            }
            balk = Rectangle {
                x: 0
                y: bind stage.height-40
        	width: bind stage.width
                height: 40
                cursor: Cursor.MOVE;
                onMousePressed: function(e) {
                    stageDragInitialX = e.screenX - stage.x;
                    stageDragInitialY = e.screenY - stage.y;
                }
                onMouseDragged: function(e) {
                    stage.x = e.screenX - stageDragInitialX;
                    stage.y = e.screenY - stageDragInitialY;
                }
                fill: Color.BURLYWOOD
            }
          magnifierImageView = ImageView {
                x: 5
                y: 5
                fitWidth: bind boundsOfRect.boundsInParent.width
                fitHeight: bind boundsOfRect.boundsInParent.height
                preserveRatio: true
                cursor: Cursor.HAND
                clip : bgRect
                image: Image {
                    backgroundLoading: false
                }
                onKeyPressed: function(e) {
                    if(e.code == KeyCode.VK_RIGHT){
                        stageX+=10
                    }
                    else if(e.code == KeyCode.VK_LEFT){
                        stageX-=10
                    }
                    else if(e.code == KeyCode.VK_UP) {
                        stageY-=10
                    }
                    else if(e.code == KeyCode.VK_DOWN) {
                        stageY+=10
                    }
                    else if(e.code == KeyCode.VK_P){
                          video.pause();
                    }
                 }
         }
        menuBalk = Group{
             blendMode: BlendMode.MULTIPLY
             content: [balk, starter, pauser, stopper]
        }
        capturerGroup = Group {
            focusTraversable : true
            content: [ magnifierImageView, lenseImgView, menuBalk, closer]
        }
        FocusRequester{};
        stage = Stage {
            title : "Scherm opnemer"
            style: StageStyle.TRANSPARENT
            x: bind stageX with inverse
            y: bind stageY with inverse
            width : bind lenseImgView.image.width
            height : bind lenseImgView.image.height
            scene : Scene {
                content: capturerGroup
                fill: Color.TRANSPARENT
            }
            onClose: function(){println("stop");}
        }
    } // end init
    function updateImage() {
        bounds = new java.awt.Rectangle(stageX + 5, (stageY+5), boundsOfRect.boundsInParent.width-12 , boundsOfRect.boundsInParent.height-45);
    }
}
// loool  dees is toch al een beetje bet er jeeeeee =)  xD dfsdfs
function run() {
    var magnifier = ScreenCap{};
    magnifier.updateImage();
    magnifier.magnifierImageView.requestFocus();
}
